package com.kingdee.shr.custom.funservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.custom.utils.DateTools;
import com.kingdee.custom.utils.StringTools;
import com.kingdee.eas.custom.buscostcount.BusCostCountEntryCollection;
import com.kingdee.eas.custom.buscostcount.BusCostCountEntryFactory;
import com.kingdee.eas.custom.buscostcount.BusCostCountEntryInfo;
import com.kingdee.eas.custom.buscostcount.IBusCostCountEntry;
import com.kingdee.shr.compensation.CalSchemeInfo;
import com.kingdee.shr.compensation.app.formula.data.Tools;
import com.kingdee.util.StringUtils;

/**
 * 
 * Title: CalCustomFunctionService
 * <p>
 * Description: 自定义核算函数
 * 
 * @author yacong_liu Email:yacong_liu@kingdee.com
 * @date 2019-8-5 & 下午04:38:07
 * @since V1.0
 * @deprecated
 */
public class CalCustomFunctionService {

    /**
     * 
     * <p>
     * Title: getBusCost
     * </p>
     * <p>
     * Description:
     * </p>
     * 
     * @param typeNum 调整类型编码 * @param paramMap
     * @return 调整费用
     */
    public double getBusCost(String typeNum, Map paramMap) {

        List tempList = new ArrayList();
        String month = String.valueOf(DateTools.getCurrentMonth());// 当前月

        Map tempMap = new HashMap();
        String tableid = (String) paramMap.get("T_HR_SCMPCALTABLE_FID");
        String personid = (String) paramMap.get("T_HR_SCMPCALTABLE_FPERSONID");
        if ((StringUtils.isEmpty(month)) || (StringUtils.isEmpty(personid)) || (StringUtils.isEmpty(tableid))
                || (StringUtils.isEmpty(typeNum))) {
            return 0.0D;
        }

        String fixMapKey = "initBusCost" + month + typeNum;
        if (tempMap.get(fixMapKey) == null) {
            try {
                tempMap.put(fixMapKey, initBusCostMap(month, typeNum));
            } catch (Exception e) {
                e.printStackTrace();
                return 0.0D;
            }
        }

        tempList = (List) tempMap.get(fixMapKey);
        Iterator iterator = tempList.iterator();

        Double money = 0.00D;
        while (iterator.hasNext()) {
            Map map = (Map) iterator.next();
            String fPersonId = (String) map.get("personId");
            String fTypeNum = (String) map.get("adjustTypeNum");
            String afterAdjust = (String) map.get("afterAdjust");
            Date bizDate = (Date) map.get("bizDate");
            String year = String.valueOf(DateTools.getYear(bizDate));
            String currentYear = String.valueOf(DateTools.getCurrentYear());

            if (fPersonId.equals(personid) && fTypeNum.equals(typeNum) && year.equals(currentYear)) {
                money = Double.valueOf(afterAdjust);
                break;
            }
        }

        return money;

    }

    /**
     * 
     * <p>
     * Title: initBusCostMap
     * </p>
     * <p>
     * Description: 初始化班车费用
     * </p>
     * 
     * @param month 调整月
     * @param typeNum 调整类型
     * @return
     * @throws BOSException
     */
    private Object initBusCostMap(String month, String typeNum) throws BOSException {

        List list = new ArrayList();
        // 查询视图 员工 调整月份 调整后费用 调整类型
        IBusCostCountEntry instance = BusCostCountEntryFactory.getLocalInstance(Tools.getInstance().getCtx());
        BusCostCountEntryCollection collection = instance.getBusCostCountEntryCollection(getEntityViewInfo(
                month, typeNum));

        Iterator iterator = collection.iterator();
        while (iterator.hasNext()) {
            Map busCostMap = new HashMap();
            BusCostCountEntryInfo entry = (BusCostCountEntryInfo) iterator.next();
            busCostMap.put("personId", StringTools.convertObjectToString(entry.getPerson().getId()));
            busCostMap.put("personName", entry.getPerson().getName());
            busCostMap.put("adjustTypeId", StringTools.convertObjectToString(entry.getAdjustType().getId()));
            busCostMap.put("adjustTypeNum", entry.getAdjustType().getNumber());
            busCostMap.put("adjustTypeName", entry.getAdjustType().getName());
            busCostMap.put("beforeAdjust", entry.getBeforeAdjust());
            busCostMap.put("afterAdjust", entry.getAfterAdjust());
            busCostMap.put("adjustMonth", entry.getAdjustMonth());
            busCostMap.put("billId", StringTools.convertObjectToString(entry.getBill().getId()));
            busCostMap.put("bizDate", entry.getBill().getBizDate());

            list.add(busCostMap);
        }

        return list;
    }

    /**
     * <p>
     * Title: getEntityViewInfo
     * </p>
     * <p>
     * Description: 班车费用分录查询视图
     * </p>
     * 
     * @param month 调整月份
     * @param typeNum 调整类型编码
     * @return
     */
    private EntityViewInfo getEntityViewInfo(String month, String typeNum) {
        EntityViewInfo viewInfo = new EntityViewInfo();
        FilterInfo filter = viewInfo.getFilter();
        filter.getFilterItems().add(new FilterItemInfo("AdjustMonth", month, CompareType.EQUALS));
        filter.getFilterItems().add(new FilterItemInfo("AdjustType.number", typeNum, CompareType.EQUALS));

        viewInfo.getSelector().add(new SelectorItemInfo("person.id"));
        viewInfo.getSelector().add(new SelectorItemInfo("person.name"));
        viewInfo.getSelector().add(new SelectorItemInfo("AdjustType.id")); // 调整类型
        viewInfo.getSelector().add(new SelectorItemInfo("AdjustType.number"));
        viewInfo.getSelector().add(new SelectorItemInfo("AdjustType.name"));
        viewInfo.getSelector().add(new SelectorItemInfo("BeforeAdjust"));// 调整前费用
        viewInfo.getSelector().add(new SelectorItemInfo("AfterAdjust"));// 调整后费用
        viewInfo.getSelector().add(new SelectorItemInfo("AdjustMonth"));// 调整月份
        viewInfo.getSelector().add(new SelectorItemInfo("AdjustDes"));// 调整说明
        viewInfo.getSelector().add(new SelectorItemInfo("bill.id")); // 单据头
        viewInfo.getSelector().add(new SelectorItemInfo("bill.bizDate")); // 业务日期
        return viewInfo;
    }
}
